<html xmlns="http://www.w3.org/1999/xhtml" xmlns:th="http://www.thymeleaf.org"
      xmlns:sec="http://www.thymeleaf.org/thymeleaf-extras-springsecurity3">
<head>
    <title>档案移交添加</title>
    <script src="/scripts/boot.js"></script>
         <!--//上传插件-->
    <link href="/scripts/webuploader/css/webuploader.css" rel="stylesheet" type="text/css" />
    <link href="/scripts/webuploader/css/style.css" rel="stylesheet" type="text/css" />
    <script src="/scripts/webuploader/js/webuploader.js"></script>
    <script src="/scripts/webuploader/js/upload.js"></script>
</head>
<body class="bg-white">
<div id="addForm">
    <table class="editTable table-bg" style="width:100%;table-layout:fixed;" border="0" cellspacing="1" cellpadding="1">
        <colgroup>
            <col width="130px"></col>
            <col width="50%"></col>
            <col width="130px"></col>
            <col width="50%"></col>
        </colgroup>
        <tr>
            <td class="text-titlebg">计划名称</td>
            <td>
                <input type="hidden" class="mini-hidden" name="id"/>
                <input name="planName" class="mini-textbox w220" readonly="readonly" vtype="maxLength:50" errorMode="none" onvalidation="onNullValidation" requiredErrorText="请输入"/></select>
            </td>
            <td class="text-titlebg">执行人</td>
            <td>
                <input name="exeName" class="mini-textbox w220" readonly="readonly" vtype="maxLength:50" errorMode="none" onvalidation="onNullValidation" requiredErrorText="请输入"/></select>
            </td>
        </tr>
        <tr>
            <td class="text-titlebg noEmpty w-166px">计划状态</td>
            <td>
                <input name="riskState" id="riskState" class="mini-textbox w220" readonly="readonly" vtype="maxLength:50" errorMode="none" onvalidation="onNullValidation" requiredErrorText="请输入"/>
            </td>
        </tr>
        <tr>
            <td class="text-titlebg noEmpty"><span class="color-red">*</span>问题描述</td>
            <td colspan="3" class="wp">
                <textarea id="problem" name="problem" class="mini-textarea wtextarea" maxlength='500' required="true" errorMode="none" onvalidation="onNullValidation" requiredErrorText="请输入"></textarea>
            </td>
        </tr>
   <tr>
            <td class="text-titlebg noEmpty">备注</td>
            <td colspan="3" class="wp">
                <textarea id="remark" name="remark" class="mini-textarea wtextarea" maxlength='500'  required="false" errorMode="none"   ></textarea>
            </td>
        </tr>
    </table>
    
    <div class="attachment-box">
        <div class="attachment-title">
            <div class="attachment-text">
                <span class="blue-dot"></span>附件
            </div>
        </div>
        <div class="attachment-content">
            <div id="file_ll" class="webuploader-container">
                <a class="up-botton" onclick="view.uploadSup()"><span class="add-bg"></span>上传</a>
                <span id="span"></span>
            </div>
            <input name="fileUrl" id="file_url" class="mini-hidden"  />
            <input name="fileName" id="file_name" class="mini-hidden" />
        </div>
        <!--<div class="attachment-content">-->
            <!--<div id="uploader" class="webuploader-container"></div>-->
            <!--<div class="attachment-content">-->
		    		<!--<div id="file_ll" class="updata-file"></div>-->
				<!--</div>-->
            <!--<input name="fileUrl" id="file_url" class="mini-hidden"  />-->
            <!--<input name="fileName" id="file_name" class="mini-hidden" />-->
        <!--</div>-->
    </div>
    <div class="title-info"><span>督导方案</span>
        <div class="position-rel">
            <div class="position-abs">
                <a onclick="view.addRow()" class="newly" iconCls="icon-upload">新增</a>
                 <!--<a class="delAll" onclick="view.removeRow()">删除</a>-->
            </div>
        </div>
    </div>
    <div id="datagrid1" class="mini-datagrid" style="width:100%;height:280px; margin-top: 35px;" idField="id"
         allowResize="true" pageSize="20" allowCellEdit="true" showPager="true" allowCellSelect="true" multiSelect="true" editNextOnEnterKey="true"  editNextRowCell="true">
        <div property="columns">
            <!--<div type="checkcolumn"></div>-->
            <div type="indexcolumn" headerAlign="center" width="20">编号</div>
            <div field="scheme" headerAlign="center" name="scheme" width="100" allowSort="true" >方案名称
                <input property="editor" class="mini-textbox"  minValue="0" maxValue="200" value="25" style="width:100%;" />
            </div>
            <div name="action" width="80" headerAlign="center" allowSort="true">操作</div>
        </div>
    </div>
    <div class="chose-toolbar">
        <a class="chose-button" onclick="CloseWindow('close')">取消</a>
        <a class="chose-button save-button" onclick="view.save(0)">保存</a>
        <a class="chose-button save-button" onclick="view.save(1)">督导上报</a>
    </div>
</div>
<script type="text/javascript">
top["win"]=window;
var grid,form,delIds="",editIds="";
var view = {
    init: function () {
        mini.parse();
        form = new mini.Form("#addForm");
        grid = mini.get("datagrid1");
        // grid.load();
        grid.on("celleditenter", function (e) {
            var index = grid.indexOf(e.record);
            if (index == grid.getData().length - 1) {
                var row = {};
                grid.addRow(row);
            }
        });
        grid.on("beforeload", function (e) {
            if (grid.getChanges().length > 0) {
                if (confirm("有增删改的数据未保存，是否取消本次操作？")) {
                    e.cancel = true;
                }
            }
        });
        grid.on(
            "drawcell",
            function(e) {
                var column = e.column;
                var record = e.record;
                if (column.name == "action") {
                    e.cellStyle = "text-align:center";
                    e.cellHtml = '<a class="del-botton" href="javascript:view.delRows()">删除</a>'
                }

        });
        grid.on("celleditenter", function(e) {
            var index = grid.indexOf(e.record);
            if (index == grid.getData().length - 1) {
                var row = {};
                grid.addRow(row);
            }
        });

        grid.on("beforeload", function(e) {
            if (grid.getChanges().length > 0) {
                if (confirm("有增删改的数据未保存，是否取消本次操作？")) {
                    e.cancel = true;
                }
            }
        });
    },
    //添加行
    addRow: function () {
        var row = {};
        grid.addRow(row, 0);
        grid.cancelEdit();
        grid.beginEditRow(row);
    },
    //删除行
    removeRow: function () {
        var rows = grid.getSelecteds();
        if (rows.length > 0) {
            grid.removeRows(rows, true);
        }
    },
    //保存
    save: function (status) {
        form.validate();
        //新增
        var infoss = grid.getChanges("added");
        var arr = '';
        if(infoss.length>1){
            for(i in infoss){
                var info = infoss[i];
                var  scheme =  info.scheme;
                if(scheme!=undefined){
                    arr += scheme+'@_@';
                }
            }
            arr = arr.substring(0, arr.lastIndexOf('@_@'));
        }else if(infoss.length==1){
            arr = infoss[0].scheme
        }
        //编辑数据
        var editInfo = grid.getChanges("modified");
        var editArr = '',editIds = '';
        if(editInfo.length>1){
            for(i in infoss){
                var info = editInfo[i];
                var  scheme =  info.scheme;
                var  id =  info.id;
                if(scheme!=undefined){
                    editArr += scheme+'@_@';
                    editIds += id+',';
                }
            }
            editArr = editArr.substring(0, editArr.lastIndexOf('@_@'));
            editIds = editIds.substring(0, editIds.lastIndexOf(','));
        }else if(editInfo.length==1){
            editArr = editInfo[0].scheme;
            editIds = editInfo[0].id;
        }
        if (form.isValid() == false) return;
        var data = form.getData();      //获取表单多个控件的数据
        data.newScheme=arr;
        data.scheme=editArr;
        data.status=status;
        data.planNodeId=data.id;
        data.delIds=delIds.substring(0, delIds.lastIndexOf(','));
        data.editIds=editIds;
        // delete data["id"];
        $.ajax({
            type : "post",
            url : "/supervise/problem/edit",
            data : data,
            success : function(msg) {
                CloseWindow("ok");
            }
        });
    },
    // 设置行
    setRows: function (rows) {
        var gridData = grid.getData();
        for (var i = 0; i < gridData.length; i++) {
            for (var j = 0; j < rows.length; j++) {
                if (gridData[i].id == rows[j].id) {
                    delete rows[j]
                }
            }
        }

        grid.addRows(rows);
    },
    //删除行
    delRows: function() {
        var rows = grid.getSelecteds();
        console.log(rows);
        if (rows.length > 0) {
            delIds += rows[0].id+',';
            grid.removeRows(rows, true);
        }
    },
    SetData:function (data) {
        data = mini.clone(data);
        $.ajax({
            url:"/supervise/problem/info?id=" + data.id,
            type: "post",
            success: function (text) {
                //跨页面传递的数据对象，克隆后才可以安全使用
                datas = mini.decode(text);   //反序列化成对象
                form.setData(datas);
                mini.get('riskState').setValue(datas.riskState==1?"有风险":"失控");
                if(text){
                    var div = $("#file_ll");
                    var listFile = text.listFile;
                    for(var i=0;i<listFile.length;i++ ){
                        var file = listFile[i];
                        var fileid = file.id;
                        var a = "<div class='file-box' id='" + file.id + "'>"+
    	    			"<a class='file-name' onclick=\"downFile('" +file.fileUrl+"','"+file.fileName+"')\">" + file.fileName + "</a>"+
    	    			 "<span class='img-wh' onclick=\"view.delFile('" +fileid+"','"+file.fileUrl+"')\"></span>"+
        				"</div>";
                        div.append(a);

                    }
                }
                grid.setData(datas.listsp)
            }
        });
    },
    uploadSup:function(){
        mini.open({
            url: "/upload/index.html",
            title: "上传附件",
            width: 1100,
            height: 500
        });
    },
    //上传
    setFile_url: function(url,num){
   		 var old_url=mini.get('file_url').getValue();
            if(old_url!=""){
                old_url=old_url+";"
            }
            mini.get('file_url').setValue(old_url+url);
    },
    setFile_name: function(name, num) {
           var old_name=mini.get('file_name').getValue();
           if(old_name!=""){
               old_name=old_name+";"
           }
           mini.get('file_name').setValue(old_name+name);

           var span = $("#span");
           span.append(name+"  ;");
    },

//     downFile: function(url, name) {
//     	debugger
//        window.location.href = "/ftp/downFile?path=" + url
//            + "&fileName=" + name;
//     },
    delFile: function(fid, url) {
       $.ajax({
           type : "post",
           url : "/fileCommon/del?id=" + fid + "&file_url=" + url,
           success : function(msg) {
               if (msg.state) {
                   $('#' + fid).remove();
               }
           }
       });
    },
    
//     onLoadFile : function(id){
//     	$.ajax({
//             url: "/fileCommon/loadFileList",
//             type : "post",
//             dataType: 'json',
//             async: false,       //是否同步,默认true异步
//             data:{"tableId":id,"tableName":"t_supervise_problem"},
//         }).done(function(text) {
//             if(text){
//                 var div = $("#file_ll");
//                 var listFile = text;
//                 for(var i=0;i<listFile.length;i++ ){
//                     var file = listFile[i];
//                     var a = "<div class='file-box' id='" + file.id + "'>"+
// 	    			"<a class='file-name' onclick=\"downFile('" +file.fileUrl+"','"+file.fileName+"')\">" + file.fileName + "</a>"+
// 	    			// "<span class='img-wh' onclick=\"delFile('" +fileid+"','"+url+"')\"></span>"+
//     				"</div>";
//                     div.append(a);

//                 }
//             }
//         });
    	
//     },
    downFile:function(url,name){
        window.location.href = "/ftp/downFile?path="+url+"&fileName="+name;
    },
};
view.init();
</script>
</body>
</html>